const util = require('utils/util.js')
const Monitor = require('utils/monitor.js').Monitor;

const postData = wx.getExtConfigSync() ? wx.getExtConfigSync().styles[0].index : {};
// const postData = {
//   app_id: 'Z2k%3D',
//   app_name: "成人",
//   backgroundColors: "#ffffff",
//   wx_appid: "wxbcc0abdeaaf6bbe5",
//   // wxaee92e370b7812a9
//   // tel: 13115601311
// }
// const postData = {
//   app_id: '5',

//   app_name: "haerbin成人",
//   backgroundColors: "#ffffff",
//   wx_appid: "wx7dc1f013e76774fb",

//   // wxaee92e370b7812a9
//   // tel: 13115601311
// }
// const postData = {
//   app_id: '35',
//   app_name: "前夫",
//   backgroundColors: "#ffffff",
//   wx_appid: "wx68d462c80b143f68",
// }
App(Monitor.hookApp({
  globalData: {
    postData,
    imgUrl: 'https://o6wndwjxn.qnssl.com/',
    imgUrls: 'https://oss.ztwlxx.cn/',
    URL: 'https://api.ztwlxx.cn/',
    indexArr: '',
    app_id: postData.app_id,
    app_name: postData.app_name,
    bgColors: postData.backgroundColors,
    wx_appid: postData.wx_appid,
    tabberArr: []
  },
  onLaunch: function () {
  
  
  },

  authorization() {
    return new Promise(function (resolve, reject) {
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: function (res) {
                resolve(res.userInfo)
              }
            })
          } else {
            resolve(false)
          }
        }
      })
    })
  },
  getPagesurl(my, pageNum) {
    var that = this
    if (that.globalData.tabberArr.length != 0) {
      this.getAFter(my)
    } else {
      this.getARR().then(res => {
        this.getAFter(my)
      })
    }
  },
  getAFter(my) {
    var that = this
    var pages = getCurrentPages();
    var Page = pages[pages.length - 1];
    var route = that.globalData.tabberArr.imgUrls.filter(res => {
      if (my == 'my') {
        return res.link_url == "/pages/mine/mine"
      } else {
        return res.link_url == "/" + Page.route
      }
    })
    if (route.length == 0) {
      return false
    }
    Page.setData({
      item: that.globalData.tabberArr,
      substance: route[0].substance,
    })
  },
  getARR(){
    var that = this
    return new Promise(function (resolve, reject) {
      util.API('wechat/Authorize/get_online').then(res => {
        let {
          code,
          data,
          msg
        } = res
        if (code === 200) {
          that.globalData.indexArr = JSON.parse(data.json).ext
          that.globalData.indexArr.styles[0].index.elements.filter(ele => {
            if (ele.type == 29) {
              that.globalData.tabberArr = ele
            }
          })

          console.log(that.globalData.indexArr.styles[0].index.elements)
          resolve(that.globalData.indexArr)
        }
      })
    })
  }, 
  sendFromId(id) {
    var that = this
    util.API('wechat/authorize/redis_form', { form_id: id }).then(res => {
      let {
        code,
        data,
        msg
      } = res
  
    })
  },
}))